package com.example.kuaizu.exception;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import kuaizu.common.constp.KuaiZuConst;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: wangmx15
 * @date: 2025/10/3
 **/
@Slf4j
public class SendMassageExcepTion extends RuntimeException {
    private int code;
    private String message;
    private SendMassageExcpTionClass sendMassageExcpTionClass;

    public SendMassageExcepTion(String str) {
        this.setSendMassageExcpTionClass(str);
        this.code = KuaiZuConst.ERROR;
    }

    public class SendMassageExcpTionClass {
        private Integer errcode;
        private String errmsg;

        public Integer getErrcode() {
            return errcode;
        }

        public void setErrcode(Integer errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }

        @Override
        public String toString() {
            return "SendMassageExcpTionClass{" +
                    "errcode=" + errcode +
                    ", errmsg='" + errmsg;
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public SendMassageExcpTionClass getSendMassageExcpTionClass() {
        return sendMassageExcpTionClass;
    }

    public void setSendMassageExcpTionClass(String str) {
        //将json的str转成对象
        try {
            //{"errcode":47003,"errmsg":"argument invalid! data.thing5.value is empty rid: 68df453d-4c78de1b-24cea0b7"}
            ObjectMapper objectMapper = new ObjectMapper();
            this.sendMassageExcpTionClass = objectMapper.readValue(str, SendMassageExcpTionClass.class);
        } catch (JsonProcessingException e) {
            log.error("json转换失败");
            this.sendMassageExcpTionClass = null;
        }

    }

    public String getMessage() {
        this.message = this.sendMassageExcpTionClass.getErrmsg();
        return this.message;
    }

    @Override
    public String toString() {
        return "SendMassageExcepTion{" +
                "code=" + code +
                ", sendMassageExcpTionClass=" + sendMassageExcpTionClass +
                '}';
    }
}
